package at.chl.genetic.function.math;

import at.chl.genetic.function.IGeneticFunction;
import at.chl.genetic.function.math.node.AbstractMathDecisionNode;
import at.chl.genetic.function.math.node.FunctionNode;
import at.chl.genetic.function.math.request.MathFunctionRequest;
import at.chl.genetic.function.math.result.MathFunctionResult;
import at.chl.genetic.node.IDecisionNode;

public class AddFunction extends AbstractMathFunction{
	
	private static final int DEFAULT_CHILDCOUNT = 2;
	private static final String NAME = "ADD";
	private int childCount;
	private FunctionNode parentNode;
	
	public AddFunction(int childCount){
		super(NAME);
		this.childCount=childCount;
		
	}
	public AddFunction(){
		this(DEFAULT_CHILDCOUNT);
	}

	
	public void setParentNode(FunctionNode parentNode) {
		this.parentNode = parentNode;
	}
	@Override
	public MathFunctionResult doFunction(MathFunctionRequest request) {
		//EXC nullpointer parent
		int sum=0;
		
		//sum all child's results 
		for(IDecisionNode node:parentNode.getChildNodes()){
			sum+=((AbstractMathDecisionNode)node).evaluate(request).getResult();
		}
		
		return new MathFunctionResult(sum);
	}

	@Override
	public int getChildCount() {
		
		return childCount;
	}
	@Override
	public IGeneticFunction cloneFunction() {
		IGeneticFunction clone=new AddFunction(this.childCount);
		return clone;
	}



}
